package mango.orm;

import java.util.HashMap;
import java.util.Map;

import mango.orm.connect.Connector;
import mango.orm.mysql.MySqlBinder;
import mango.orm.mysql.MySqlExtracter;
import mango.orm.mysql.MySqlSQLMaker;
import mango.orm.oracle.OracleBinder;
import mango.orm.oracle.OracleExtracter;
import mango.orm.oracle.OracleSQLMaker;



public final class OrmFactory {
    private static Map<String, Binder> BINDER = new HashMap<String, Binder>();
    private static Map<String, Extracter> EXTRACTER = new HashMap<String, Extracter>();
    private static Map<String, SQLMaker> SQLMAKER = new HashMap<String, SQLMaker>();
    
    public static final Binder getBinder(String dbType) {
    	Binder binder = BINDER.get(dbType.toUpperCase());
    	if(binder == null) {
    		if(Connector.DB_ORACLE.equalsIgnoreCase(dbType)) {
    			binder = new OracleBinder();
    		}
    		else if (Connector.DB_MYSQL.equalsIgnoreCase(dbType)) {
    			binder = new MySqlBinder();
    		}
    		else {
    			throw new IllegalArgumentException("Error DataBase Type \"" + dbType + "\"");
    		}
    		BINDER.put(dbType.toUpperCase(), binder);
    	}
    	return binder;
    }
    
    public static final Extracter getExtracter(String dbType) {
    	Extracter extracter = EXTRACTER.get(dbType.toUpperCase());
    	if(extracter == null) {
    		if(Connector.DB_ORACLE.equalsIgnoreCase(dbType)) {
    			extracter = new OracleExtracter();
    		}
    		else if (Connector.DB_MYSQL.equalsIgnoreCase(dbType)) {
    			extracter = new MySqlExtracter();
    		}
    		else {
    			throw new IllegalArgumentException("Error DataBase Type \"" + dbType + "\"");
    		}
    		EXTRACTER.put(dbType.toUpperCase(), extracter);
    	}
    	return extracter;
    }
    
    public static final SQLMaker getSQLMaker(String dbType) {
    	SQLMaker sqlMaker = SQLMAKER.get(dbType.toUpperCase());
    	if(sqlMaker == null) {
    		if(Connector.DB_ORACLE.equalsIgnoreCase(dbType)) {
    			sqlMaker = new OracleSQLMaker();
    		}
    		else if (Connector.DB_MYSQL.equalsIgnoreCase(dbType)) {
    			sqlMaker = new MySqlSQLMaker();
    		}
    		else {
    			throw new IllegalArgumentException("Error DataBase Type \"" + dbType + "\"");
    		}
    		SQLMAKER.put(dbType.toUpperCase(), sqlMaker);
    	}
    	return sqlMaker;
    }
}
